package com.css.fxfzypg.modules.resultsManagement.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.resultsManagement.entity.MapTemplate;
import com.css.fxfzypg.modules.resultsManagement.service.MapTemplateService;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mapTemplate")
@Slf4j
public class MapTemplateController {
    @Resource
    MapTemplateService mapTemplateService;
    //新增
    @PostMapping("/saveMapTemplate")
    public RestResponse saveMap(@RequestBody MapTemplate mapTemplate) {
        RestResponse restResponse = null;
        try {
            restResponse = mapTemplateService.saveMapTemplate(mapTemplate);
        } catch (Exception e) {
            log.error("新增图件模板数据失败", e);
            restResponse = RestResponse.fail("新增图件模板数据失败!");
        }
        return restResponse;
    }
    // id查询
    @GetMapping("/getById/{id}")
    public RestResponse getById(@PathVariable("id") String id) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(mapTemplateService.getById(id));
        } catch (Exception e) {
            log.error("查询图件模板数据失败", e);
            restResponse = RestResponse.fail("查询图件模板数据失败");
        }
        return restResponse;
    }
    //删除
    @PostMapping("/deleteById/{id}")
    public RestResponse deleteById(@PathVariable String id) {
        RestResponse restResponse = null;
        try {
            mapTemplateService.deleteById(id);
            restResponse = RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error("删除图件模板数据失败");
            restResponse = RestResponse.fail("删除图件模板数据失败");
        }
        return restResponse;
    }
    //修改
    @PostMapping("/update")
    public RestResponse update(@RequestBody MapTemplate mapTemplate) {
        RestResponse restResponse = null;
        try {
            MapTemplate mapTemplate1 = mapTemplateService.update(mapTemplate);
            restResponse = RestResponse.succeed(mapTemplate1);
        } catch (Exception e) {
            log.error("修改图件模板数据失败");
            restResponse = RestResponse.fail("修改图件模板数据失败");
        }
        return restResponse;
    }
    //分页查询
    @GetMapping("/queryMapTemplates")
    public RestResponse queryMapTemplates(HttpServletRequest request, MapTemplate mapTemplate) {
        RestResponse response = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> map = mapTemplateService.queryMapTemplates(mapTemplate, curPage, pageSize);
            response = RestResponse.succeed(map);
        } catch (Exception e) {
            log.error("查询图件模板数据失败!", e);
            response = RestResponse.fail("查询图件模板数据失败!");
        }
        return response;
    }

    @GetMapping("/query")
    public RestResponse query(@RequestParam String reportType) {
        RestResponse restResponse = null;
        try {
            List<Map<String,Object>> list=mapTemplateService.query(reportType);
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    @GetMapping("/getTask")
    public RestResponse getTask(@RequestParam String reportType) {
        RestResponse restResponse = null;
        try {
            List list=mapTemplateService.getTask(reportType);
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }
}
